using Microsoft.AspNetCore.SignalR;
using WMS.RealTime.IServers.Interfaces;

namespace WMS.RealTime.API.Hubs
{
    /// <summary>
    /// 大屏数据推送Hub
    /// </summary>
    /// 继承关系  Hub基础类--用于连接和分组   IDashboardHub--定义接口,和业务实现方法
    public class DashboardHub : Hub, IDashboardHub
    {
        private readonly ILogger<DashboardHub> _logger;

        public DashboardHub(ILogger<DashboardHub> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 连接建立时
        /// </summary>
        public override async Task OnConnectedAsync()
        {
            _logger.LogInformation($"客户端连接: {Context.ConnectionId}");
            await base.OnConnectedAsync();
        }

        /// <summary>
        /// 连接断开时
        /// </summary>
        public override async Task OnDisconnectedAsync(Exception? exception)
        {
            _logger.LogInformation($"客户端断开: {Context.ConnectionId}");
            await base.OnDisconnectedAsync(exception);
        }

        /// <summary>
        /// 加入大屏组
        /// </summary>
        public async Task JoinDashboardGroup()
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, "dashboard");
            _logger.LogInformation($"客户端 {Context.ConnectionId} 加入大屏组");
        }

        /// <summary>
        /// 离开大屏组
        /// </summary>
        public async Task LeaveDashboardGroup()
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, "dashboard");
            _logger.LogInformation($"客户端 {Context.ConnectionId} 离开大屏组");
        }

        /// <summary>
        /// 发送今日概况数据
        /// </summary>
        public async Task SendTodayOverviewAsync(object data)
        {
            await Clients.Group("dashboard").SendAsync("ReceiveTodayOverview", data);
        }

        /// <summary>
        /// 发送昨日概况数据
        /// </summary>
        public async Task SendYesterdayOverviewAsync(object data)
        {
            await Clients.Group("dashboard").SendAsync("ReceiveYesterdayOverview", data);
        }

        /// <summary>
        /// 发送库存占比数据
        /// </summary>
        public async Task SendInventoryProportionAsync(object data)
        {
            await Clients.Group("dashboard").SendAsync("ReceiveInventoryProportion", data);
        }

        /// <summary>
        /// 发送入库统计数据
        /// </summary>
        public async Task SendInboundStatisticsAsync(object data)
        {
            await Clients.Group("dashboard").SendAsync("ReceiveInboundStatistics", data);
        }

        /// <summary>
        /// 发送待办事项数据
        /// </summary>
        public async Task SendTodoItemsAsync(object data)
        {
            await Clients.Group("dashboard").SendAsync("ReceiveTodoItems", data);
        }

        /// <summary>
        /// 发送告警数据
        /// </summary>
        public async Task SendAlertDataAsync(object data)
        {
            await Clients.Group("dashboard").SendAsync("ReceiveAlertData", data);
        }

        /// <summary>
        /// 发送排名数据
        /// </summary>
        public async Task SendRankingDataAsync(object data)
        {
            await Clients.Group("dashboard").SendAsync("ReceiveRankingData", data);
        }

        /// <summary>
        /// 发送系统消息
        /// </summary>
        public async Task SendSystemMessageAsync(string message)
        {
            await Clients.Group("dashboard").SendAsync("ReceiveSystemMessage", message);
        }

        /// <summary>
        /// 发送系统消息（原有方法，保持兼容）
        /// </summary>
        public async Task SendSystemMessage(string message)
        {
            await SendSystemMessageAsync(message);
            _logger.LogInformation($"发送系统消息: {message}");
        }
    }
} 